<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;



use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Logics\CategoryLogic;
use App\Logics\BrandLogic;
use App\Logics\GoodsLogic;
use App\Logics\GoodsImgLogic;
use App\Logics\SupplierLogic;

use Illuminate\Support\Facades\Storage;
class BrandController extends BaseController{

    private function getInstance(){
        static $ob=null;
        if(is_null($ob)){
            $ob= new BrandLogic();
        }
        return $ob;
    }
    //分类列表
    public function index()
    {
        $page = (int)request("page", 1);
        $per_page = (int)request("per_page", 20);

        $brand_name = trim(request('brand_name'));
        $brand_id = (int)request('brand_id',0);
        $brand_is_del = (int)request('brand_is_del',-1);
        $is_official_auth = (int)request('is_official_auth',-1);

        $para=[
            'page'=>$page,
            'per_page'=>$per_page,
            'brand_name'=>$brand_name,
            'brand_id'=>$brand_id,
            'brand_is_del'=>$brand_is_del,
            'is_official_auth'=>$is_official_auth
        ];
         $ob= $this->getInstance();
         $arr=$ob->index($para);
         if($arr && is_string($arr)) die(json_error($arr));

        $content= view("admin.brand.index",$arr) ;

        $data=[
            'content'=>$content,
            'header'=>'分类列表',
        ];
        return  $this->baseList($data);
    }



    //分类添加
    public function add(Request $request){
        if($_POST){
            $brand_name=trim(request('brand_name'));
            $brand_note=trim(request('brand_note'));
            $is_official_auth=(int)request('is_official_auth');
            $is_official_auth==1?1:2;
            $para=[
                'brand_name'=>$brand_name,
                'brand_note'=>$brand_note,
                'is_official_auth'=>$is_official_auth,
            ];
            $ob=$this->getInstance();
            $data=$ob->add($para,$request);
            $this->result($data);
        }

        $content= view("admin.brand.add") ;
        $data=[
            'content'=>$content,
            'header'=>'品牌添加',
        ];
        return  $this->baseList($data);
    }

    public function edit(Request $request){
        if($_POST){
            $brand_name=trim(request('brand_name'));
            $brand_note=trim(request('brand_note'));
            $brand_id=(int)request('brand_id');
            $is_official_auth=(int)request('is_official_auth');
            $brand_is_del=(int)request('brand_is_del');
            $is_official_auth==1?1:2;
            $brand_is_del==1?1:2;

            $para=[
                'brand_name'=>$brand_name,
                'brand_note'=>$brand_note,
                'is_official_auth'=>$is_official_auth,
                'brand_id'=>$brand_id,
                'brand_is_del'=>$brand_is_del
            ];
            $ob=$this->getInstance();
            $data=$ob->edit($para,$request);
            $this->result($data);
        }


        $brand_id=(int)request('brand_id');
        $data= BrandLogic::getOne($brand_id);
        $url=config('sys.img_url').'/';
        $data['brand_logo']=$data['brand_logo']?$url.$data['brand_logo']:$data['brand_logo'];
        $content= view("admin.brand.edit",['data'=>$data]) ;
        $data=[
            'content'=>$content,
            'header'=>'品牌编辑',
        ];
        return  $this->baseList($data);
    }


    //删除品牌数据
    public function delete(){
        if($_POST){
            $brand_id=(int)request('brand_id');
            $ob=$this->getInstance();
            $data=$ob->delete($brand_id);
            $this->result($data);
        }
    }



    //删除分类的图片
    public function delImg()
    {
        if($_POST){
            $brand_id=(int)request('brand_id');
            $ob=$this->getInstance();
            $data=$ob->delImg($brand_id);
            $this->result($data);
        }
        return '失败';
    }

    //字段更新
    public function ajaxFieldsEdit(){
        $brand_id=(int)request('brand_id');
        $type=trim(request('type',''));

        $para=[
            'brand_id'=>$brand_id,
            'type'=>$type
        ];
         $data=BrandLogic::ajaxFieldsEdit($para);
         $this->result($data);
    }





}